#!/bin/bash

# initial configuration
config_file="$(dirname "$0")/extdata/config.ini"
version="v$(grep "^\[intraNetSeeker\]" -A 1 "$config_file" | grep "version" | cut -d "=" -f 2)"
current_date=$(date +"%Y%m%d")

# Function to show software information
function software_info() {
    CYAN='\033[0;36m' # Light cyan
    NC='\033[0m'     # Reset Colors

    echo -e "${CYAN}  ___       _             _   _      _   ____            _      "
    echo " |_ _|_ __ | |_ _ __ __ _| \ | | ___| |_/ ___|  ___  ___| | _____ _ __    "
    echo "  | || '_ \| __| '__/ _\` |  \| |/ _ \ __\___ \ / _ \/ _ \ |/ / _ \ '__|  "
    echo "  | || | | | |_| | | (_| | |\  |  __/ |_ ___) |  __/  __/   <  __/ |      "
    echo " |___|_| |_|\__|_|  \__,_|_| \_|\___|\__|____/ \___|\___|_|\_\___|_|      "
    echo ""
    echo "By: Enthusiasm23"

    echo "Version: $version"
    echo -e "Gitee: https://gitee.com/enthusiasm23/IntraNetSeeker ${NC}"
    echo ""
 }


# Function to show help message
function show_help() {
    echo "Thank you for using intraNetSeeker software. Supported commands are:"
    echo "    seek md5c combine concat convenc"
}


# Function to find Python3 path
function find_python3_path() {
    local python3_path

    # Check if python3 is available as an alias
    alias_python3=$(alias python3 2>/dev/null)

    if [[ -n $alias_python3 ]]; then
        # Use grep and sed to extract the path from the alias
        python3_path=$(echo "$alias_python3" | grep -o "/[^']*" | sed "s/'//g")
    else
        # Find the path to python3 when it's directly available
        python3_path=$(which python3)
    fi

    # If python3 is available directly or as an alias, echo the path
    if [[ -n $python3_path ]]; then
        echo "$python3_path"
    else
        # Use a default Python path from configuration or raise an error if unavailable
        config_python3=$(grep "^\[software\]" -A 1 "$config_file" | grep "python3" | cut -d "=" -f 2)
        if [[ -n $config_python3 ]]; then
            echo "$config_python3"
        else
            echo "ERROR: Python3 is not installed, and no default path is specified in the configuration file."
            exit 1
        fi
    fi

}


# Check if running on a Linux system
if [ "$(uname)" != "Linux" ]; then
    echo "This script is only supported on Linux."
    exit 1
fi

# Check if no options are provided
if [[ $# -eq 0 ]]; then
    software_info
    echo "Usage: intraNetSeeker -h/--help"
    exit 1
fi

# Set the INTRANETSEEKER environment variable to the script's directory
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

# Call the function to find Python3 path
python3=$(find_python3_path)

while [[ $# -gt 0 ]]; do
    case $1 in
        seek)
            shift
            # Execute the 'seek' subcommand script
            "$SCRIPT_DIR/scripts/fileSeek.sh" "$@"
            exit 0
            ;;
        md5c)
            shift
            # Execute the 'clean' subcommand script
            "$SCRIPT_DIR/scripts/fileMD5c.sh" "$@"
            exit 0
            ;;
        combine)
            shift
            # Execute the 'combine' subcommand script
            $python3 "$SCRIPT_DIR/scripts/fileCombiner.py" "$@"
            exit 0
            ;;
        concat)
            shift
            # Execute the 'combine' subcommand script
            "$SCRIPT_DIR/scripts/fileConcat.sh" "$@"
            exit 0
            ;;
        convenc)
            shift
            # Execute the 'combine' subcommand script
            "$SCRIPT_DIR/scripts/fileConvenc.sh" "$@"
            exit 0
            ;;
        -h|--help)
            show_help
            exit 0
            ;;
        -v|--version)
            software_info
            exit 0
            ;;
        *)
            echo "ERROR: Unrecognized command: $1"
            echo ""
            software_info
            show_help
            exit 1
            ;;
    esac
done
